<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Settle;
use App\Http\Model\FinanceKk;

class UnionSettleAppointUnionMake extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'UnionSettleAppointUnionMake';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '对公结算数据单独某个公会的生成';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected $unionTable = 'union';
    protected $unionSettleTable = 'union_settle';
    protected $financeWalletTable = 'finance_wallet';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $time = time();
        $settle_date_st = $time;
        $settle_date_ed = $time;
        $unionId = 1110;

        $settleModel = new Settle();
        $modelFinanceKk = new FinanceKk();
        $ratioConf = config('pay');

        do {
            //所有对公结算公会
            $unionList = DB::table($this->unionTable)
                ->where('id', $unionId)
                ->where('settle_type', 2)
                ->limit(1)
                ->get();
            $unionList = $this->objToArray($unionList);
            foreach($unionList as $val) {
                //会长余额
                $kkBalance = DB::table($this->financeWalletTable)->where('user_id', $val['un_user_id'])->first();
                $kkBalance = $this->objToArray($kkBalance);
                if(empty($kkBalance['kk_balance'])) {
                    continue;
                }
                $kkBalance = empty($kkBalance['kk_balance']) ? 0 : $kkBalance['kk_balance'];
                $desc = $ratioConf['wallet_change_desc']['kk_union_settle'];

                //结算记录
                $log = [];
                $log['union_id'] = $val['id'];
                $log['user_id'] = $val['un_user_id'];
                $log['settle_date_st'] = $settle_date_st;
                $log['settle_date_ed'] = $settle_date_ed;
                $log['settle_kk'] = $kkBalance;
                $log['union_company'] = $val['union_company'];
                $log['union_bank_name'] = $val['union_bank_name'];
                $log['union_bank_no'] = $val['union_bank_no'];
                $log['settle_docx'] = '';
                $log['status'] = 0;

                DB::beginTransaction();
                try {
                    //扣余额
                    $modelFinanceKk->inc($val['un_user_id'], 0, 0, 36, 0-$kkBalance, $desc);
                    //结算记录
                    $settleModel->insertUnionSettle($log);
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    write_log('error_unionSettleCommand', '异常', ['val'=>$val, 'log'=>$log, 'message'=>$e->getMessage(), 'line'=>$e->getLine()]);
                    continue;
                }
            }

        } while (false);

    }

    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

}